<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-date-picker v-model="queryParams.year" style="width: 160px" type="year" value-format="yyyy"
          placeholder="请选择年份" clearable size="small" :picker-options="year" @keyup.enter.native="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否含税" prop="isTax">
        <el-select v-model="queryParams.isTax" clearable size="small">
          <el-option v-for="dict in taxOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="月份" prop="ovmonth">
        <el-select v-model="queryParams.ovmonth" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:orderkpi:export']">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderkpiList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="年份" align="center" prop="orderyear" />
      <el-table-column label="成本类型" align="center" prop="orderCostType" />
      <el-table-column label="订单号" align="center" prop="orderCode" :show-overflow-tooltip="true" />
      <el-table-column label="订单名称" align="center" prop="orderName" :show-overflow-tooltip="true" />
      <el-table-column label="产值(元)" align="center" prop="countOutPutMoney" :show-overflow-tooltip="true" />
      <el-table-column label="借款(元)" align="center" prop="countBorrowAmount" :show-overflow-tooltip="true" />
      <el-table-column label="报销(元)" align="center" prop="countRAmount" :show-overflow-tooltip="true" />
      <el-table-column label="工资(元)" align="center" prop="salaryCount" :show-overflow-tooltip="true" />
      <el-table-column label="成本合计(元)" align="center" width=100px prop="costCountAmount" />
      <el-table-column label="产值成本比%" align="center" width=100px prop="costRatio" />
      <el-table-column label="目标完成比%" align="center" width=100px prop="targetRatio" />

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产值kpi对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="成本类型">
          <el-select v-model="form.orderCostType" placeholder="请选择成本类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单名称" prop="orderName">
          <el-input v-model="form.orderName" placeholder="请输入订单名称" />
        </el-form-item>
        <el-form-item label="产值总金额" prop="countOutputMoney">
          <el-input v-model="form.countOutputMoney" placeholder="请输入产值总金额" />
        </el-form-item>
        <el-form-item label="借款总金额" prop="countBorrowAmount">
          <el-input v-model="form.countBorrowAmount" placeholder="请输入借款总金额" />
        </el-form-item>
        <el-form-item label="报销总金额" prop="countRAmount">
          <el-input v-model="form.countRAmount" placeholder="请输入报销总金额" />
        </el-form-item>
        <el-form-item label="工资总金额" prop="salaryCount">
          <el-input v-model="form.salaryCount" placeholder="请输入工资总金额" />
        </el-form-item>
        <el-form-item label="补贴总金额" prop="countSubsidyAmount">
          <el-input v-model="form.countSubsidyAmount" placeholder="请输入补贴总金额" />
        </el-form-item>
        <el-form-item label="成本合计" prop="costCountAmount">
          <el-input v-model="form.costCountAmount" placeholder="请输入成本合计" />
        </el-form-item>
        <el-form-item label="订单年份" prop="orderyear">
          <el-input v-model="form.orderyear" placeholder="请输入订单年份" />
        </el-form-item>
        <el-form-item label="产值成本比" prop="costRatio">
          <el-input v-model="form.costRatio" placeholder="请输入产值成本比" />
        </el-form-item>
        <el-form-item label="目标完成比" prop="targetRatio">
          <el-input v-model="form.targetRatio" placeholder="请输入目标完成比" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderkpi, getOrderkpi, delOrderkpi, addOrderkpi, updateOrderkpi, exportOrderkpi } from "@/api/system/orderkpi";

export default {
  name: "Orderkpi",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产值kpi表格数据
      orderkpiList: [],
      //是否含税
      taxOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCostType: null,
        orderCode: null,
        orderName: null,
        countOutputMoney: null,
        countBorrowAmount: null,
        countRAmount: null,
        salaryCount: null,
        countSubsidyAmount: null,
        costCountAmount: null,
        orderyear: null,
        costRatio: null,
        targetRatio: null
      },
      //查询月份
      options: [
        {
          value: "1",
          label: "1月",
        },
        {
          value: "2",
          label: "2月",
        },
        {
          value: "3",
          label: "3月",
        },
        {
          value: "4",
          label: "4月",
        },
        {
          value: "5",
          label: "5月",
        },
        {
          value: "6",
          label: "6月",
        },
        {
          value: "7",
          label: "7月",
        },
        {
          value: "8",
          label: "8月",
        },
        {
          value: "9",
          label: "9月",
        },
        {
          value: "10",
          label: "10月",
        },
        {
          value: "11",
          label: "11月",
        },
        {
          value: "12",
          label: "12月",
        },
      ],
      value: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_kpi_isTax").then((response) => {
      this.taxOptions = response.data;
    });
  },
  methods: {
    /** 查询产值kpi列表 */
    getList() {
      this.loading = true;
      listOrderkpi(this.queryParams).then(response => {
        this.orderkpiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCostType: null,
        orderCode: null,
        orderName: null,
        countOutputMoney: null,
        countBorrowAmount: null,
        countRAmount: null,
        salaryCount: null,
        countSubsidyAmount: null,
        costCountAmount: null,
        orderyear: null,
        costRatio: null,
        targetRatio: null
      };
      this.resetForm("form");
    },
    /**字典含税*/
    stateFormat(row, column) {
      return this.selectDictLabel(this.taxOptions, row.state);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产值kpi";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderkpi(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产值kpi";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderkpi(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOrderkpi(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除产值kpi编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delOrderkpi(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产值kpi数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportOrderkpi(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () { });
    }
  }
};
</script>
